package com.fancy.learncenter.ui.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonEveryDayPreviewDataBean;
import com.fancy.learncenter.common.MAudioPlayer;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonTeacherEveryDayPreviewAdapter;
import com.fancy.learncenter.ui.adapter.CartoonTeacherEveryDayWordPreviewAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

import static com.fancy.learncenter.ui.activity.CartoonTeacherClassDetailActivity.CLASS_ID_KEY;
import static com.fancy.learncenter.ui.activity.CartoonTeacherEveryDayDetailActivity.STUDENT_ID_KEY;

public class CartoonTeacherEveryDayPreviewActivity extends BaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    RecyclerView word_recyclerView;


    boolean isPlay = false;
    MAudioPlayer recordPlayer;
    int cusPosition = -1;
    String classId = "";
    String studentId = "";
    String studenName = "";
    @Bind(R.id.user_name_text)
    TextView userNameText;
    @Bind(R.id.complete_accurate)
    TextView completeAccurate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_every_day_preview, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("跟读详情");
        classId = getIntent().getStringExtra(CLASS_ID_KEY);
        studentId = getIntent().getStringExtra(STUDENT_ID_KEY);
        studenName = getIntent().getStringExtra("studenName");
        userNameText.setText(studenName);


        initView();

        getTeacherClassList();
    }

    CartoonTeacherEveryDayPreviewAdapter previewAdapter;

    CartoonTeacherEveryDayWordPreviewAdapter wordPreviewAdapter;

    View topView;

    private void initView() {

        initTopView();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        previewAdapter = new CartoonTeacherEveryDayPreviewAdapter(this, new ArrayList<CartoonEveryDayPreviewDataBean.ResultBean.GrammerSpeakListBean>());
        recyclerView.setAdapter(previewAdapter);
        previewAdapter.setHeaderView(topView, recyclerView);


    }

    private void initTopView() {
        topView = LayoutInflater.from(this).inflate(R.layout.activity_cartoon_everyday_preview_detail_top, null);
        word_recyclerView = topView.findViewById(R.id.word_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        word_recyclerView.setLayoutManager(linearLayoutManager);

        wordPreviewAdapter = new CartoonTeacherEveryDayWordPreviewAdapter(this, new ArrayList<CartoonEveryDayPreviewDataBean.ResultBean.WordSpeakListBean>());
        wordPreviewAdapter.setClickCallBack(new CartoonTeacherEveryDayWordPreviewAdapter.ClickCallBack() {

            @Override
            public void clickCallBack(CartoonEveryDayPreviewDataBean.ResultBean.WordSpeakListBean item, final SimpleDraweeView simpleDraweeView, int position) {

                if (cusPosition == position && recordPlayer.isPlaying()) {
                    recordPlayer.stop();
                } else {
                    if (recordPlayer != null) {
                        recordPlayer.stop();
                    }

                    recordPlayer = new MAudioPlayer();
                    recordPlayer.setAudioPlayerStatus(new MAudioPlayer.AudioPlayerStatus() {
                        @Override
                        public void playerPause() {
                            if (simpleDraweeView != null) {
                                simpleDraweeView.setImageURI(Uri.parse("res://com.fancy.learncenter/" + R.mipmap.preview_voice));
                            }
                        }

                        @Override
                        public void playerCompletion(MediaPlayer mp) {
                            simpleDraweeView.setImageURI(Uri.parse("res://com.fancy.learncenter/" + R.mipmap.preview_voice));
                            isPlay = false;
                        }

                        @Override
                        public void playerPrepared(MediaPlayer mp) {
                            recordPlayer.play();
                        }

                        @Override
                        public void playerStart() {
                            String urlPath = "res://com.fancy.learncenter/" + R.mipmap.cartoon_word_preview;
                            DraweeController draweeController =
                                    Fresco.newDraweeControllerBuilder()
                                            .setUri(urlPath)
                                            .setOldController(simpleDraweeView.getController())
                                            .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
                                            .build();
                            simpleDraweeView.setController(draweeController);

                        }

                        @Override
                        public void playerError(MediaPlayer mp, int what, int extra) {
                            isPlay = false;
                            simpleDraweeView.setImageURI(Uri.parse("res://com.fancy.learncenter/" + R.mipmap.preview_voice));
                        }
                    });
                    recordPlayer.playUrl(item.getAnswerResourceUrls().get(0));
                }

                cusPosition = position;


            }
        });
        word_recyclerView.setAdapter(wordPreviewAdapter);
    }

    @Override
    public void finish() {
        super.finish();

        if (recordPlayer != null && recordPlayer.isPlaying()) {
            recordPlayer.stop();
        }

    }


    private void getTeacherClassList() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("studentId", studentId);
        map.put("queryType", 1);

        HttpMehtod.getInstance().everyDayPreView(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {


                BaseBean<CartoonEveryDayPreviewDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonEveryDayPreviewDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonEveryDayPreviewDataBean>() {
                    @Override
                    public void success(CartoonEveryDayPreviewDataBean dataBean) {

                        int num = 0;
                        if (dataBean.getResult() != null) {
                            if (dataBean.getResult().getWordSpeakList() != null) {
                                wordPreviewAdapter.notifyDataSetChanged(dataBean.getResult().getWordSpeakList());
                                num = num + dataBean.getResult().getWordSpeakList().size();
                            }

                            if (dataBean.getResult().getGrammerSpeakList() != null) {
                                previewAdapter.notifyDataSetChanged(dataBean.getResult().getGrammerSpeakList());
                                num = num + dataBean.getResult().getGrammerSpeakList().size();
                            }

                            completeAccurate.setText(num + "道");
                        }


                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }
}
